//
//  NSString+Check.m
//  CarService
//
//  Created by Nimo.Yu on 15/1/30.
//  Copyright (c) 2015年 lexingsoft. All rights reserved.
//

#import "NSString+Check.h"

#import "NSString+Tools.h"
#import "PromptAction.h"

@implementation NSString (Check)

+ (BOOL) checkNullString:(NSString *)checkString
{
    BOOL isNullString = YES;
    if (checkString && ![checkString isKindOfClass:[NSNull class]] &&![checkString isEqualToString:@"<null>"] && ![checkString isEqualToString:@"(null)"] && ![checkString isEqualToString:@""])
    {
        isNullString = NO;
    }
    return isNullString;
}

///检查是否是有效的车牌号码
+ (BOOL) checkIsCarNoString:(NSString *)checkString
{
    NSString *match=@"(^[\u4E00-\u9FA5][0-9a-zA-Z]{6}$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:checkString];
}

+(BOOL) checkIsChineseString:(NSString *)checkString{

    NSString *match=@"(^[\u4e00-\u9fa5]+$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:checkString];
}

#pragma mark - check string

+ (BOOL)isValidatePhoneNumber:(NSString *)_strPhone
{
    _strPhone=[_strPhone stringByReplacingOccurrencesOfString:@"+86" withString:@""];
    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"- "];
    _strPhone = [[_strPhone componentsSeparatedByCharactersInSet: doNotWant]componentsJoinedByString:@""];
    NSString *regex = @"^((13[0-9])|(147)|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:_strPhone];
    if (!isMatch) {
        return NO;
    }
    return YES;
}


+ (BOOL) checkPhoneString:(NSString *) phone
{
    BOOL result = YES;
    if ([NSString checkNullString:phone]) {
        [PromptAction showErrorPrompt:@"手机号不能为空!"];
        result = NO;
    }else if(![NSString isValidatePhoneNumber:phone]){
        [PromptAction showErrorPrompt:@"手机号格式不正确"];
        result = NO;
    }
    return result;
}

+(BOOL)checkEqualString:(NSString *)_strString1 withString:(NSString *)_strString2{

    BOOL result = YES;
    if ([NSString checkNullString:_strString1]) {
        [PromptAction showErrorPrompt:@"密码不能为空!"];
        result = NO;
    }else if ([NSString checkNullString:_strString2]){
        
        [PromptAction showErrorPrompt:@"确认密码不能为空!"];
        result = NO;
    
    }else if (![_strString1 isEqualToString:_strString2]){
        
        [PromptAction showErrorPrompt:@"两次密码输入不一致!"];
        result = NO;
    
    }
    
    return result;
}

///身份证号
+ (BOOL) validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

///邮箱
+ (BOOL)isValidateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
    
    return [emailTest evaluateWithObject:email];
}
@end
